package com.gu.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gu.domain.dto.TagListDto;
import com.gu.domain.entity.ResponseResult;
import com.gu.domain.entity.Tag;
import com.gu.domain.service.TagService;
import com.gu.domain.mapper.TagMapper;
import com.gu.domain.utils.BeanCopyUtils;
import com.gu.domain.vo.PageVo;
import com.gu.domain.vo.TagVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 34389
* @description 针对表【sg_tag(标签)】的数据库操作Service实现
* @createDate 2025-10-30 09:22:28
*/
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag>
    implements TagService{

    @Override
    public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        //分页查询

        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(tagListDto.getName()), Tag::getName, tagListDto.getName());
        queryWrapper.eq(StringUtils.hasText(tagListDto.getRemark()), Tag::getRemark, tagListDto.getRemark());

        Page<Tag> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        page(page, queryWrapper);
        //封装数据返回
        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    //删除标签
    @Override
    public ResponseResult removeTags(String ids) {
        try {
            // 参数校验
            if (!StringUtils.hasText(ids)) {
                return ResponseResult.errorResult(400, "参数不能为空");
            }
            // 解析ID列表
            List<Long> idList = Arrays.stream(ids.split(","))
                    .map(String::trim)
                    .filter(StringUtils::hasText)
                    .map(Long::valueOf)
                    .collect(Collectors.toList());

            if (idList.isEmpty()) {
                return ResponseResult.errorResult(400, "无效的参数");
            }

            // 执行删除
            boolean result;
            if (idList.size() == 1) {
                result = this.removeById(idList.get(0));
            } else {
                result = this.removeByIds(idList);
            }

            if (result) {
                return ResponseResult.okResult("删除成功");
            } else {
                return ResponseResult.errorResult(500, "删除失败");
            }

        } catch (NumberFormatException e) {
            return ResponseResult.errorResult(400, "参数格式错误，请提供有效的数字ID");
        } catch (Exception e) {
            return ResponseResult.errorResult(500, "删除失败，系统异常");
        }
    }


    //---------------------------写博文-查询文章标签的接口---------------------------
    @Override
    public List<TagVo> listAllTag() {
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Tag::getId,Tag::getName);
        List<Tag> list = list(wrapper);
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(list, TagVo.class);
        return tagVos;
    }


}




